<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:NobleTech.Products.CreatureKingdom.WPFClient.Controls"
>   
    <c:DivideByMinusTwoConverter x:Key="DivideByMinusTwoConverter"/>
    <c:DivideByTwoConverter x:Key="DivideByTwoConverter"/>
    <c:MinusRadToDegreeConverter x:Key="MinusRadToDegreeConverter"/>
    <c:NegativeConverter x:Key="NegativeConverter"/>
    <c:IsAliveToOpacityConverter x:Key="IsAliveToOpacityConverter"/>
    <c:BoolToIntConverter x:Key="BoolToIntConverter"/>
    <c:MultiplyConverter x:Key="MultiplyConverter"/>
    
    <DrawingBrush x:Key="GridBrush" Viewport="0,0,10,10" ViewportUnits="Absolute" TileMode="Tile">
        <DrawingBrush.Drawing>
            <DrawingGroup Opacity="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DisplayGrid, Converter={StaticResource BoolToIntConverter}}">
                <DrawingGroup.Children>
                    <GeometryDrawing Geometry="M0,0 L1,0 1,0.02, 0,0.02Z" Brush="DarkRed"/>
                    <GeometryDrawing Geometry="M0,0 L0,1 0.02,1, 0.02,0Z" Brush="DarkRed" />
                </DrawingGroup.Children>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>
    <Style TargetType="{x:Type c:Map}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type c:Map}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            ClipToBounds="True" Name="PART_MapBorder">
                        <Canvas Name="MyCanvas" Background="{TemplateBinding Background}"  RenderTransformOrigin="0,0" Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=MapSizeX}" Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=MapSizeY}" HorizontalAlignment="Left" VerticalAlignment="Top" >
                            <Canvas.RenderTransform>
                                <TransformGroup>
                                    <TranslateTransform X="{Binding RelativeSource={RelativeSource TemplatedParent},Path=MapPositionX, Converter={StaticResource NegativeConverter}}" Y="{Binding RelativeSource={RelativeSource TemplatedParent},Path=MapPositionY, Converter={StaticResource NegativeConverter}}"/>
                                    <ScaleTransform ScaleX="{Binding RelativeSource={RelativeSource TemplatedParent},Path=MapZoom}" ScaleY="{Binding RelativeSource={RelativeSource TemplatedParent},Path=MapZoom}"/>
                                    <TranslateTransform X="{Binding ElementName=PART_MapBorder, Path=ActualWidth, Converter={StaticResource DivideByTwoConverter}}" Y="{Binding ElementName=PART_MapBorder, Path=ActualHeight, Converter={StaticResource DivideByTwoConverter}}"/>
                                </TransformGroup>
                            </Canvas.RenderTransform>                          
                            <ItemsPresenter/>                            
                        </Canvas>                        
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="DisplayGrid" Value="True">
                            <Setter TargetName="MyCanvas"  Property="Background" Value="{StaticResource GridBrush}"/>
                        </Trigger>                    
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>            
        </Setter>
        
        <Setter Property="ItemsPanel">            
            <Setter.Value>
                <ItemsPanelTemplate>
                    <Canvas/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Rectangle Name="OrgRectangle" Width="1" Height="1" Fill="{Binding Path=Texture}" Opacity="{Binding Path=IsAlive, Converter={StaticResource IsAliveToOpacityConverter}}" Stroke="Red" StrokeThickness="0">
                        <Rectangle.RenderTransform>
                            <TransformGroup>
                                <RotateTransform Angle="{Binding Path=DirectionFacing, Converter={StaticResource MinusRadToDegreeConverter}}" CenterX="0.5" CenterY="0.5"/>
                                <ScaleTransform ScaleX="{Binding Path=Size}" ScaleY="{Binding Path=Size}"/>
                                <TranslateTransform X="{Binding Path=PositionX}" Y="{Binding Path=PositionY}"/>
                                <TranslateTransform X="{Binding Path=Size, Converter={StaticResource DivideByMinusTwoConverter}}" Y="{Binding Path=Size, Converter={StaticResource DivideByMinusTwoConverter}}"/>
                            </TransformGroup>
                        </Rectangle.RenderTransform>
                    </Rectangle>
                    <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                                <Setter TargetName="OrgRectangle" Property="StrokeThickness" Value="0.1"/>
                            </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </Setter.Value>
        </Setter>
   
    </Style>
    <Style TargetType="{x:Type c:MapSelector}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type c:MapSelector}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            ClipToBounds="True" Name="PART_MapBorder">
                        <Canvas>
                            <Rectangle Width="10" Height="30" Name="PART_Selector" Stroke="Red" StrokeThickness="2">
                                <Rectangle.Fill>
                                    <SolidColorBrush Color="Black" Opacity="0.2"/>
                                </Rectangle.Fill>                                    
                            </Rectangle>
                        </Canvas>
                    </Border>                    
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>
